package com.wuliu.driver.service;

import com.wuliu.driver.mapper.CarMapper;
import com.wuliu.driver.mapper.DriveMapper;
import com.wuliu.driver.mapper.DriverRealnameMapper;
import com.wuliu.driver.mapper.DriverUserMapper;
import com.wuliu.driver.pojo.Car;
import com.wuliu.driver.pojo.DriverRealname;
import com.wuliu.driver.util.Code;
import com.wuliu.driver.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class DriverRealnameServiceImpl implements DriverRealnameService{
    @Autowired
    private DriverRealnameMapper driverRealnameMapper;
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private DriveMapper driveMapper;
    @Autowired
    private DriverUserMapper driverUserMapper;
    //提交审核
    @Override
    public Result ins(DriverRealname driverRealname) {
        //先去数据库中查询是否有该用户的审核信息
        Integer select = driverRealnameMapper.select(driverRealname.getDid());
        if (select!=null){
            int i = driverRealnameMapper.updStatus(driverRealname.getDid());
            if (i>0){
                return new Result(Code.GET_OK,driverRealname,"提交成功");
            }
            return new Result(Code.GET_ERR,"提交失败");
        }else {
            int ins = driverRealnameMapper.ins(driverRealname);
            if (ins>0){
                return new Result(Code.GET_OK,driverRealname,"提交成功");
            }
            return new Result(Code.GET_ERR,"提交失败");
        }

    }

    //查询该用户的所有审核
    @Override
    public Result select(Integer did) {
        Integer carStatus = carMapper.select(did);
        Integer rStatus = driverRealnameMapper.select(did);
        Integer dStatus = driveMapper.select(did);
        System.out.println("carStatus:"+carStatus+"rStatus:"+rStatus+"dStatus:"+dStatus);
        Map<String,Object> map = new HashMap<>();
        if (carStatus == null){
            carStatus =3;
        }
        if (rStatus == null){
            rStatus =3;
        }
        if (dStatus == null){
            dStatus =3;
        }
        map.put("carStatus",carStatus);
        map.put("rStatus",rStatus);
        map.put("dStatus",dStatus);
        if (carStatus == 1 && rStatus == 1 && dStatus == 1){
            driverUserMapper.updStatus(did);
        }
        return new Result(Code.GET_OK,map,"查询成功");
    }
}
